#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include "iot_gpio_ex.h"
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_uart.h"
#include "hi_uart.h"
#include "iot_watchdog.h"
#include "iot_errno.h"
#include "uart_control.h"

#define UART_BUFF_SIZE 100
#define U_SLEEP_TIME   100000

unsigned char d_d ;
unsigned char d_x ;
unsigned char d_y ;
unsigned char n_d = 0;

void Uart1GpioInit(void)
{
    IoTGpioInit(IOT_IO_NAME_GPIO_6);
    // 设置GPIO0的管脚复用关系为UART1_TX Set the pin reuse relationship of GPIO0 to UART1_ TX
    IoSetFunc(IOT_IO_NAME_GPIO_6, IOT_IO_FUNC_GPIO_6_UART1_TXD);
    IoTGpioInit(IOT_IO_NAME_GPIO_5);
    // 设置GPIO1的管脚复用关系为UART1_RX Set the pin reuse relationship of GPIO1 to UART1_ RX
    IoSetFunc(IOT_IO_NAME_GPIO_5, IOT_IO_FUNC_GPIO_5_UART1_RXD);
}

void Uart1Config(void)
{
    uint32_t ret;
    /* 初始化UART配置，波特率 9600，数据bit为8,停止位1，奇偶校验为NONE */
    /* Initialize UART configuration, baud rate is 9600, data bit is 8, stop bit is 1, parity is NONE */
    IotUartAttribute uart_attr = {
        .baudRate = 115200,
        .dataBits = 8,
        .stopBits = 1,
        .parity = 0,
    };
    ret = IoTUartInit(HI_UART_IDX_1, &uart_attr);
    if (ret != IOT_SUCCESS) {
        printf("Init Uart1 Falied Error No : %d\n", ret);
        return;
    }
}

static void UartTask(void)
{
    const char *data = "Hello OpenHarmony !!!\n";
    uint32_t count = 0;
    uint32_t len = 0;
    unsigned char uartReadBuff[UART_BUFF_SIZE] = {0};
    // 对UART1的一些初始化 Some initialization of UART1
    Uart1GpioInit();
    // 对UART1参数的一些配置 Some configurations of UART1 parameters
    Uart1Config();

    while (1) {

        
        // 通过UART1 发送数据 Send data through UART1
        IoTUartWrite(HI_UART_IDX_1, (unsigned char*)data, strlen(data));
        // 通过UART1 接收数据 Receive data through UART1
        len = IoTUartRead(HI_UART_IDX_1, uartReadBuff, UART_BUFF_SIZE);
         // 检查帧头和帧尾
        if (len >= 6 && uartReadBuff[0] == 0xAA && uartReadBuff[1] == 0x55 && uartReadBuff[5] == 0xFF) {
            // 提取数据并赋值给变量
            d_d = (int)uartReadBuff[2];
            d_x = (int)uartReadBuff[3];
            d_y = (int)uartReadBuff[4];

            // 打印或使用这些变量
            printf("d_d: %d, d_x: %d, d_y: %d\n", d_d, d_x, d_y);
        } else {
            n_d++;
            if(n_d >= 2)
            {
                d_d = 0;
                n_d = 0;                
            }
            printf("Packet format error!!%d\n",n_d);
        }
        // if (len > 0) {
        //     // 把接收到的数据打印出来 Print the received data
        //     printf("Uart Read Data is: [ %d ] %s \r\n", count, uartReadBuff);
        // }
        usleep(U_SLEEP_TIME);
        count++;
    }
}

void UartExampleEntry(void)
{
    osThreadAttr_t attr;
    IoTWatchDogDisable();

    attr.name = "UartTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 5 * 1024; // 任务栈大小*1024 stack size 5*1024
    attr.priority = osPriorityNormal;

    if (osThreadNew((osThreadFunc_t)UartTask, NULL, &attr) == NULL) {
        printf("[UartTask] Failed to create UartTask!\n");
    }
}

APP_FEATURE_INIT(UartExampleEntry);
